package cn.wolfcode.ssm.web.controller;


import cn.wolfcode.ssm.domain.Business;
import cn.wolfcode.ssm.enums.AppointmentStatusEnum;
import cn.wolfcode.ssm.enums.BusinessReportGroupTpyeEnum;
import cn.wolfcode.ssm.enums.ConsumptionStatusEnum;
import cn.wolfcode.ssm.qo.BusinessQueryObject;
import cn.wolfcode.ssm.qo.BusinessReportQueryObject;
import cn.wolfcode.ssm.qo.JsonResult;
import cn.wolfcode.ssm.service.IBusinessReportService;
import cn.wolfcode.ssm.service.IBusinessService;
import cn.wolfcode.ssm.util.FileUploadUtil;
import cn.wolfcode.ssm.util.RequireAnnotation;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/businessReport")
public class BusinessReportController {

    @Autowired
    private IBusinessReportService businessReportService;
    @Autowired
    private IBusinessService businessService;


    @RequireAnnotation(name = "门店报表页面",expression = "businessReport:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") BusinessReportQueryObject qo){
        PageInfo<HashMap> pageInfo = businessReportService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("businesses", businessService.listAll());
        model.addAttribute("consumptionStatusEnums", ConsumptionStatusEnum.values());
        model.addAttribute("businessGroupTypeEnums", BusinessReportGroupTpyeEnum.values());
        return "businessReport/list";
    }

    @RequireAnnotation(name = "门店报表柱状图",expression = "businessReport:bar")
    @RequestMapping("/bar")
    public String bar(Model model, @ModelAttribute("qo") BusinessReportQueryObject qo){
        List businessName = new ArrayList();
        List totalCount = new ArrayList();
        List totalAmount = new ArrayList();
        List totalDiscountAmount = new ArrayList();
        List totalPayAmount = new ArrayList();
        //得到的结果不进行分页,配置文件中设置了pageSize为0时不进行分页
        qo.setPageSize(0);
        List<HashMap> list = businessReportService.query(qo).getList();
        String typeName = BusinessReportGroupTpyeEnum.findEnumNameByValue(qo.getGroupType());
        list.forEach(hashMap -> {
            totalCount.add(hashMap.get("totalCount"));
            totalAmount.add(hashMap.get("totalAmount"));
            totalDiscountAmount.add(hashMap.get("totalDiscountAmount"));
            totalPayAmount.add(hashMap.get("totalPayAmount"));
            businessName.add(hashMap.get("businessName"));
        });
        model.addAttribute("totalCount", JSON.toJSONString(totalCount));
        model.addAttribute("totalAmount", JSON.toJSONString(totalAmount));
        model.addAttribute("totalDiscountAmount", JSON.toJSONString(totalDiscountAmount));
        model.addAttribute("totalPayAmount", JSON.toJSONString(totalPayAmount));
        model.addAttribute("businessName", JSON.toJSONString(businessName));
        model.addAttribute("typeName", typeName);
        return "businessReport/businessReportBar";
    }


}
